# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class QidianHotPipeline:
    def process_item(self, item, spider):
        # 判断小说形式是连载还是完结
        if item["form"] == "连载":
            item["form"] = "LZ"
        else:
            item["form"] = "WJ"
        return item


from scrapy.exceptions import DropItem


# 去除重复作者的 Item Pipeline
class DuplicatesPipeline(object):
    # 定义一个保存作者姓名的集合
    def __init__(self):
        self.author_set = set()

    def process_item(self, item, spider):
        if item["author"] in self.author_set:
            # 抛弃重复的Item项
            raise DropItem("查找到重复姓名的项目： %s" % item)
        else:
            self.author_set.add(item["author"])
        return item


class SaveToTxtPipline(object):
    # file_name = "hot.txt"
    file = None

    # Spider 开启时，执行卡开文件操作
    def open_spider(self, spider):
        # 这里的文件名字 也可以放到 settings.py中
        file = None  # 文件对象
        # 以追加形式打开文件
        self.file = open(self.file_name, "a", encoding="utf-8")
        # 数据处理

    @classmethod
    def from_crawler(cls, crawler):
        # 获取配置文件中的FILE_NAME 的值
        # 如果获取失败，就使用默认值 "hot_def".txt
        cls.file_name = crawler.settings.get("FILE_NAME", "hot_Def.txt")
        return cls()

    def process_item(self, item, spider):
        # 获取item中各个字段，将其连接成一个字符串
        # 获取字段用分号隔开
        # 字符串末尾要有换行符号\n
        noval_str = item["name"] + ";" + item["type"] + ";" + item["form"] + "\n"
        # 将字符串写入文件中
        self.file.write(noval_str)
        return item

    def close_spider(self, spider):
        self.file.close()
